package edu.pitt.dbmi.monarch.rest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * Author : Jeremy Espino MD
 * Created  8/28/13 2:46 PM
 */
public class MouseGenotypePhenotypeMapping implements DiseasePhenotypeMapping {

    String[] labels;
    String[] instances;


    private MouseGenotypePhenotypeMapping() {
        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void init() throws Exception {
        Map<String, HashSet<String>> mouseToPhenotypeMap = new HashMap<String, HashSet<String>>();
        Map<String, String> mouseLabelToMouseMap = new HashMap<String, String>();


        // read the mapping file
        File f = new File(Env.getInstance().getProperty(Env.MOUSE_MAP_FILE));
        FileInputStream fis = new FileInputStream(f);
        BufferedReader bf = new BufferedReader(new InputStreamReader(fis));
        String str;
        int line = 0;
        while ((str = bf.readLine()) != null) {
            line++;
            if (str.trim().length() == 0 || line == 1) {
                continue;
            }
            String[] values = str.split("\\t", -1); // don't truncate empty fields

            //System.out.println(values[1] + " | " + values[2] + " | " + values[6]);

            values[2] = values[2].replaceAll(" ","_");

            if (!mouseToPhenotypeMap.containsKey(values[1]))
                mouseToPhenotypeMap.put(values[1], new HashSet<String>());
            mouseToPhenotypeMap.get(values[1]).add(values[6]);
            mouseLabelToMouseMap.put(values[2], values[1]);
        }
        fis.close();

        labels = new String[mouseLabelToMouseMap.size() * 2];
        int i = 0;
        for (String key : mouseLabelToMouseMap.keySet()) {
            labels[i] = key;
            labels[i + 1] = mouseLabelToMouseMap.get(key);
            i = i + 2;
        }

        instances = new String[line * 2];
        i = 0;
        for (String key : mouseToPhenotypeMap.keySet()) {
            for (String val : mouseToPhenotypeMap.get(key)){
                instances[i] = key;
                instances[i+1] = val;
                i = i + 2;
            }
        }
    }

    public static DiseasePhenotypeMapping getInstance() {
        return MouseGenotypePhenotypeMappingHolder.INSTANCE;
    }

    private static class MouseGenotypePhenotypeMappingHolder {
        public static final MouseGenotypePhenotypeMapping INSTANCE = new MouseGenotypePhenotypeMapping();
    }

    public String[] getInstances() {

        return instances;
    }

    public String[] getLabels() {
        return labels;

    }

    public static void main(String[] args) throws Exception {
        MouseGenotypePhenotypeMapping.getInstance().init();
        String[] r = MouseGenotypePhenotypeMapping.getInstance().getLabels();
        for (String l : r) {
            System.out.println(l.toString());
        }

    }


}
